'use strict';

module.exports = function (grunt) {
  require('load-grunt-tasks')(grunt);
  require('time-grunt')(grunt);

  grunt.initConfig({
    yeoman: {
      theme: grunt.option('theme'),
      dist: 'dist/clt_' + grunt.option('app')
    },
    clean: {
      dist: {
        files: [{
          dot: true,
          src: [
            '.tmp',
            '<%= yeoman.dist %>/*',
            '!<%= yeoman.dist %>/.git*'
          ]
        }]
      },
      server: '.tmp'
    },
    jshint: {
        options: {
            jshintrc: '.jshintrc'
        },
        all: [
            'client/scripts/{,*/}*.js',
            'ext/themes/<%= yeoman.theme %>/widget/**/*.js',
            'ext/components/*/client/**/*.js'
        ]
    },
    neuter: {
        client: {
            options: {
                filepathTransform: function (filepath) {
                    return 'client/scripts/' + filepath;
                }
            },
            src: 'client/scripts/requires.js',
            dest: '.tmp/client/scripts/scripts-client.js'
        },
        theme: {
            options: {
                filepathTransform: function (filepath) {
                    return 'ext/themes/' + grunt.option('theme') +  '/' + filepath;
                }
            },
            src: 'ext/themes/<%= yeoman.theme %>/requires.js',
            dest: '.tmp/client/scripts/scripts-theme.js'
        }
    },
    compass: {
        options: {
            debugInfo: true,
            importPath: 'bower_components',
            httpGeneratedImagesPath: '../img/generated',
            httpFontsPath: 'fonts',
            relativeAssets: false
        },
        client: {
            options: {
                sassDir: 'client/styles',
                cssDir: '.tmp/client/styles',
                generatedImagesDir: '.tmp/client/img/generated',
                imagesDir: 'client/img',
                httpImagesPath: '../../../client/img'
            }
        },
        clientTheme: {
            options: {
                sassDir: 'ext/themes/<%= yeoman.theme %>',
                cssDir: '.tmp/client/styles',
                generatedImagesDir: '.tmp/client/img/generated',
                imagesDir: 'ext/themes/<%= yeoman.theme %>/img',
                httpImagesPath: '../../../ext/themes/<%= yeoman.theme %>/img'
            }
        }
    },
    // not used since Uglify task does concat,
    // but still available if needed
    /*concat: {
      dist: {}
    },*/
    useminPrepare: {
      html: 'client/index.html',
      options: {
        dest: '<%= yeoman.dist %>'
      }
    },

    usemin: {
        html: ['<%= yeoman.dist %>/**/*.html'],
        css: ['<%= yeoman.dist %>/styles/{,*/}*.css'],
        options: {
            dirs: ['<%= yeoman.dist %>']
        }
    },

    imagemin: {
        dist: {
            files: [{
                expand: true,
                cwd: 'client/img',
                src: ['{,*/}*.{png,jpg,jpeg}'], //, '!**/icons-*/**'],
                dest: '<%= yeoman.dist %>/img'
            }]
        }
    },

    svgmin: {
        dist: {
            files: [{
                expand: true,
                cwd: 'client/img',
                src: '{,*/}*.svg',
                dest: '<%= yeoman.dist %>/img'
            }]
        }
    },

    cssmin: {
      // By default, your `index.html` <!-- Usemin Block --> will take care of
      // minification. This option is pre-configured if you do not wish to use
      // Usemin blocks.
      // dist: {
      //   files: {
      //     '<%= yeoman.dist %>/styles/main.css': [
      //       '.tmp/styles/{,*/}*.css',
      //       'client/styles/{,*/}*.css'
      //     ]
      //   }
      // }
    },
    htmlmin: {
        dist: {
            options: {
                /*removeCommentsFromCDATA: true,
                 // https://github.com/yeoman/grunt-usemin/issues/44
                 //collapseWhitespace: true,
                 collapseBooleanAttributes: true,
                 removeAttributeQuotes: true,
                 removeRedundantAttributes: true,
                 useShortDoctype: true,
                 removeEmptyAttributes: true,
                 removeOptionalTags: true*/
            },
            files: [{
                expand: true,
                cwd: 'client',
                src: ['*.html', 'views/**/*.html'],
                dest: '<%= yeoman.dist %>'
            }]
        }
    },
    // Put files not handled in other tasks here
    copy: {
        dist: {
            files: [{
                expand: true,
                dot: true,
                cwd: 'client',
                dest: '<%= yeoman.dist %>',
                src: [
                    '*.{ico,png,txt}',
                    '.htaccess',
                    'img/{,*/}*.{gif,webp}',
                    'styles/fonts/*'
                ]
            }, {
                expand: true,
                cwd: '.tmp/img',
                dest: '<%= yeoman.dist %>/img',
                src: [
                    'generated/*'
                ]
            },
                {
                    expand: true,
                    dot: true,
                    cwd: 'bower_components/bootstrap/dist',
                    dest: '<%= yeoman.dist %>',
                    src: [
                        'fonts/**'
                    ]
                }]
        }
    },
    concurrent: {
      dist: [
          'compass:client',
          'compass:clientTheme',
          'imagemin',
          'svgmin',
          'htmlmin'
      ]
    },
    ngmin: {
        dist: {
            files: [{
                expand: true,
                cwd: '<%= yeoman.dist %>/scripts',
                src: '*.js',
                dest: '<%= yeoman.dist %>/scripts'
            }]
        }
    },
    uglify: {
        dist: {
            files: {
                '<%= yeoman.dist %>/scripts/scripts.js': [
                    '<%= yeoman.dist %>/scripts/scripts.js'
                ]
            }
        }
    },
      replace: {
          clientTheme: {
              src: ['.tmp/client/styles/requires.css'],
              overwrite: true,
              replacements: [{
                  from: 'fonts/../',
                  to: '../../../ext/themes/<%= yeoman.theme %>/'
              }]
          }
      }
  });

    grunt.registerTask('build', [
        'clean:dist',
        'useminPrepare',
        'concurrent:dist',
        'replace:clientTheme',
        'neuter:client',
        'neuter:theme',
        'concat',
        'copy:dist',
        'ngmin',
        'cssmin',
        'uglify',
        'usemin'
    ]);

    grunt.registerTask('default', [
        'jshint',
        'build'
    ]);
};
